package com.itheima.jobs;

import com.itheima.constrant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

import static com.itheima.utils.QiniuUtils.deleteFileFromQiniu;

/**
 * @author FCJ
 * @date 2020-01-04 12:29
 */
public class cleraImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void cleraImg() {
        //根据Redies中两个set集合进行差值计算，获得垃圾图片集合
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String picName : sdiff) {
                //删除七牛云的垃圾图片
              QiniuUtils.deleteFileFromQiniu(picName);
                //删除redis的垃圾图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("清理垃圾图片"+picName);
            }
        }
    }
}
